﻿using System;
using System.Windows;
using System.Windows.Media;

using SBPweb.Forge.Common.Exceptions;

using SBPweb.GoogleDataAPI.Client.Connector.Services;
using SBPweb.GoogleDataAPI.Client.Connector.Services.PhotoService;

namespace WpfTestApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        private IPhotoProvider photoProvider;

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            photoProvider = PhotoProviderFactory.CreatePhotoProviderInstance();
            photoProvider.AuthenticationSucceed += new EventHandler(photoProvider_AuthenticationSucceed);
            photoProvider.AuthenticationFailed += new EventHandler<ExceptionHandler.ExceptionEventArgs>(photoProvider_AuthenticationFailed);
            tbkLog.Text += ServiceConnector.OverriddenServiceHostRoot.OriginalString;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            photoProvider.Authenticate(tbxUserName.Text, tbxPassword.Password);
        }

        private void photoProvider_AuthenticationSucceed(object sender, EventArgs e)
        {
            photoProvider.DoStuff();
            this.Dispatcher.BeginInvoke(new Action(delegate
            {
                Application.Current.MainWindow.Background = new SolidColorBrush(Color.FromArgb(255, 0, 200, 0));
            }));
        }

        private void photoProvider_AuthenticationFailed(object sender, ExceptionHandler.ExceptionEventArgs e)
        {
            this.Dispatcher.BeginInvoke(new Action(delegate
            {
                Application.Current.MainWindow.Background = new SolidColorBrush(Color.FromArgb(255, 200, 0, 0));
            }));
        }

    }

}